import {
  getCatalog,
  brandLike
} from "../../../api.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    letterArray: [
      "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "X", "Y", "Z"
    ],
    filteredBrandList: [],
    target: '',
    sousou: false,
    ss_tit: [],
    alert_other: true,
    input_focus: false,
    top_bindinput_text:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    getCatalog(
      getApp().globalData.businessId
    ).then(res => {
      if (res.data.code == 666) {
        that.handleList(res.data.data[0].children)
      }
      console.log(res.data)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  handleList(brandList) {
    let label = brandList[0].label
    let letterArray = [label];
    let filteredBrandList = {}
    for (let i = 0, l = brandList.length; i < l; i++) {
      let brand = brandList[i]
      if (brand.label === label) {
        if (!filteredBrandList[label]) {
          filteredBrandList[label] = []
        }
        filteredBrandList[label].push(brand)
      } else {
        label = brand.label
        letterArray.push(label)
        if (!filteredBrandList[label]) {
          filteredBrandList[label] = []
        }
        filteredBrandList[label].push(brand)
      }
    }

    this.setData({
      filteredBrandList
    })
  },
  scrollTo(e) {
    let id = e.target.dataset.id
    this.setData({
      target: id
    })
  },
  brands(e) {
    var name = e.currentTarget.dataset.name
    var id = e.currentTarget.dataset.id
    var pages = getCurrentPages();
    var that = this
    if (id == 171) {
      that.setData({
        alert_other: false,
        input_focus: true
      })
      return
    }
    var prevPage = pages[pages.length - 2];
    prevPage.setData({
      brand: name,
      'form.brand_id': id,
      'form.brand_other': '',
    })
    setTimeout(function(){
      wx.navigateBack({
        delta: 1
      })
    },500)    
  },
  top_bindinput(e) {
    var text = e.detail.value
    var that = this
    if (text.length == 0) {
      that.setData({
        sousou: false
      })
    } else {
      brandLike(
        text
      ).then(res => {
        if (res.data.code == 666) {
          that.setData({
            sousou: true,
            ss_tit: res.data.data,
            top_bindinput_text:text
          })
        }
        console.log(res)
      })
    }
  },
  alert_other_close(e) {
    this.setData({
      alert_other: true
    })
  },
  submit(e) {
    var name = e.detail.value.name
    if (!name) {
      wx.showToast({
        title: '请填写名称',
        icon: "none"
      })
      return
    }
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];
    prevPage.setData({
      brand: name,
      'form.brand_other': name,
      'form.brand_id': 171
    })
    wx.navigateBack({
      delta: 1
    })
  },
  addBnad(){
    var that = this
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];
    prevPage.setData({
      brand: that.data.top_bindinput_text,
      'form.brand_other': that.data.top_bindinput_text,
      'form.brand_id': ''
    })
    wx.navigateBack({
      delta: 1
    })
  },
  onShow:function(){
    getApp().startpage(new Date().getTime())
  },
  onHide: function () {
    getApp().endpage(new Date().getTime())
  },
})